package com.iwjw.ibatis.generate.util;

import com.iwjw.ibatis.generate.model.GenerateConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 创建Mapper工具类
 *
 * @author: huangweiwei
 * @email: ihuangweiwei@163.com
 * @created:2016/10/25
 */
public class BuildMapperUtil {
    private static final String MAPPER_TEMPLATE = "" +
            "package %s;\n" +
            "\n" +
            "import %s;\n" +
            "import com.iwjw.ibatis.mapper.AutoMapper;\n" +
            "\n" +
            "/**\n" +
            " * @author %s\n" +
            " * @created %s\n" +
            " */\n" +
            "public interface %s extends AutoMapper<%s> {\n" +
            "\n" +
            "}\n";
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
    public static Logger logger = LoggerFactory.getLogger(BuildMapperUtil.class);

    public static boolean build(String pkg, String modelPkg, String model) {
        String code = String.format(MAPPER_TEMPLATE, pkg, modelPkg + "." + model, GenerateConfig.AUTHOR, dateFormat.format(new Date()), model + "Mapper", model).trim();
        logger.info("mapper:\n{}", code);
        return FileUtil.write(pkg, model + "Mapper.java", code, GenerateConfig.OVERRIDE_FILE);
    }
}
